

//#include "Nimbits.h"
//#include <Client.h>
//#include <Ethernet.h>
//#include <SPI.h>
//#include <PString.h>
//#include <stdlib.h>
 
//nimbits settings, set the instance name (nimbits-02 is the public cloud on https://cloud.nimbits.com) the email of the account owner, and a read write key they have created.
/*
String instance = "nimbits-02";
char owner[] = "bsautner@gmail.com";
String readWriteKey = "key";
byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0x54, 0x39}; //this ethernet shield's MAC address
*/

//Nimbits nimbits(instance, owner, readWriteKey);

/*
void setup() {
  Serial.begin(9600); //initialize serial communication for debugging
  if (Ethernet.begin(mac) == 0) {
    Serial.println("DHCP Failed!");
    while(true);
  }
  randomSeed(analogRead(0));
  Serial.println("Online"); 
  delay(1000);
}

void loop() { 
    delay(1000);
    nimbits.recordValue(random(300),"","bsautner@gmail.com/lab_temp"); 
}
*/

String floatToString(double number, uint8_t digits)
{
	String resultString = "";
	// Handle negative numbers
	if (number < 0.0)
	{
		resultString += "-";
		number = -number;
	}

	// Round correctly so that print(1.999, 2) prints as "2.00"
	double rounding = 0.5;
	for (uint8_t i=0; i<digits; ++i)
	rounding /= 10.0;

	number += rounding;

	// Extract the integer part of the number and print it
	unsigned long int_part = (unsigned long)number;
	double remainder = number - (double)int_part;
	resultString += int_part;

	// Print the decimal point, but only if there are digits beyond
	if (digits > 0)
	resultString += ".";

	// Extract digits from the remainder one at a time
	while (digits-- > 0)
	{
		remainder *= 10.0;
		int toPrint = int(remainder);
		resultString += toPrint;
		remainder -= toPrint;
	}
	return resultString;
}

void nimbits(double lt, double lg, double value, String note, char *pointId) {

	String json;
	json = "{\"lt\":\"";
	json += floatToString(lt, 4);
	json += "\",\"lg\":\"";
	json += floatToString(lg, 4);
	json += "\",\"d\":\"";
	json += floatToString(value, 4);
	json += "\",\"n\":\"";
	json += note;
	json += "\"}";
	String content;
	content = "email=";
	content += "stener.bonde@volleyteamodense.dk";
	content += "&key=";
	content += "bagdad";
	content += "&json=";
	content += json;
	content += "&id=";
	content += pointId;

	Serial.println(content);

	if (client.connect("cloud.nimbits.com", 80)) {
		client.println("POST /service/v2/value HTTP/1.1");
		client.println("Host:cloud.nimbits.com");
		client.println("Connection:close");
		client.println("User-Agent: Arduino/1.0");
		client.println("Cache-Control:max-age=0");
		client.println("Content-Type: application/x-www-form-urlencoded");
		client.print("Content-Length: ");
		client.println(content.length());
		client.println();
		client.println(content);
		//delay(2000);
		
		/*
		
		// Check status
		if (!client.connected()) {
			while(client.available())
			Serial.print((char)client.read());
			client.flush();
			client.stop();
			Serial.println("Not connected.");
			return;
		} else {
			Serial.println("Client is connected.");
			// Wait for response
			while (!client.available())
			;
			// Now response is available
			char c = client.read();
			Serial.print(c);
			switch(parseStatus) {
				case 0:
				if (c == ' ') parseStatus++; break;  // skip "HTTP/1.1 "
				case 1:
				if (c >= '0' && c <= '9') {
					statusCode *= 10;
					statusCode += c - '0';
				} else {
					parseStatus++;
				}
			}
			Serial.print("Status: ");
			Serial.println(statusCode);
		}
		// Client no longer needs to be connected
		client.stop();
		Serial.println("Connection to client is now stopped.");
		*/
		
		unsigned long timeNow = millis();
		while (client.connected()) {
			if (client.available()) {
				char c = client.read();
				Serial.print(c);
				if (c == '}')
					break;
			} else {
				delay(10);
			}
			if (millis() > timeNow + 10000) { // If 10 seconds passes without incoming data, we break out.
				Serial.println("Reading answer from server timed out.");
				break;
			}
		}
		Serial.println();
		Serial.println("disconnecting.");
		client.stop();

		/*
		while(client.connected() && !client.available()) {
			delay(1);
		}
		Serial.println("Server har svaret.");
		int a = client.available();
		Serial.print("Antal bytes i serverens svar: ");
		Serial.println(a);
		while (client.available() ) {
			char c = client.read();
			Serial.print(c);
		}
		*/
		
	}

}
